import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import 'jquery.nicescroll'

// 导入全局样式表
import './assets/css/global.css'

// jq 改变滚动条样式
// import 'jquery.nicescroll'

// 导入axios
// import axios from 'axios'
import instance from './plugins/axios'
// http response 拦截器
instance.interceptors.response.use(
  response => {
    // 拦截响应，做统一处理
    if (response.data.code) {
      switch (response.data.code) {
        case 1002:
          // store.state.isLogin = false
          router.replace({
            path: '/login',
            query: {
              redirect: router.currentRoute.fullPath
            }
          })
          break
        case 401:
          // 返回 401 清除token信息并跳转到登录页面
          // store.commit(types.LOGOUT)
          console.log(response)
          sessionStorage.removeItem('token')
          router.replace({
            path: '/login',
            query: { redirect: router.currentRoute.fullPath }
          })
          break
      }
    }
    return response
  },
  // 接口错误状态处理，也就是说无响应时的处理
  error => {
    // console.log(error) // 401
    if (error) {
      switch (error) {
        case 1002:
          // store.state.isLogin = false
          router.replace({
            path: '/login',
            query: {
              redirect: router.currentRoute.fullPath
            }
          })
          break
        case 401:
          // 返回 401 清除token信息并跳转到登录页面
          // store.commit(types.LOGOUT)
          sessionStorage.removeItem('token')
          router.replace({
            path: '/login',
            query: { redirect: router.currentRoute.fullPath }
          })
          break
      }
    }
    return Promise.reject(error.status) // 返回接口返回的错误信息
  }
)
// 配置axios请求的根路径
// axios.defaults.baseURL = 'http://172.28.34.35:5500/'
// axios.defaults.baseURL = 'http://localhost:3000/'
// Vue.prototype.$http = axios
Vue.prototype.$http = instance

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
